<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>	
	<title><spring:message code="message.lable.TuiHuoShengQing"/></title>
	<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
    <!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1><spring:message code="message.list.ZhiDingTuiHuoJiHua"/></h1>
        <ol class="breadcrumb">
        	<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
        	<li><a href="#">交易</a></li>
            <li><a href="#">药品退货</a></li>
            <li class="active"><a href="#"><spring:message code="message.list.ZhiDingTuiHuoJiHua"/></a></li>
        </ol>
    </section>
    
    <section class="content">
         <div class=" box box-success">
            <div class="box-body">
                <div class="input">
                	<div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="qualityspecName" name="qualityspecName" placeholder="<spring:message code="message.listForm.tongYongMing"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                   <div  hidden>
                         <div class="textalign"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
		                 <div class="boxinput">
		                    <input type="text" class="textinput" id="drugCode" name="drugCode"  placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
		                 </div>
                   </div>
                    <div class="textalign1"><spring:message code="message.listForm.PeiSongQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id=delCompName name="delCompName"  placeholder="<spring:message code="message.listForm.PeiSongQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
					<div class="textalign1"><spring:message code="message.listForm.shengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id=compName name="compName"  placeholder="<spring:message code="message.listForm.shengChanQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
					<div class="textalign1" >
						<button type="button" onclick="gridSerch();" class="btn btn-success btn-sm" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
					</div>
                 </div>
            </div>
        </div>


        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
           <button type="button" onclick="joinReturnPlan()"	class="btn btn-primary btn-sm"><spring:message code="message.button.JiaRuTuiHuoJiHua"/></button>
        </div>



    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
	//绑定回车
    $('#qualityspecName,#drugCode,#delCompName,#compName').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    $("#qualityspecName").focus();//加载页面光标在第一个查询框内
    
    
    
        $(function () {
        	$("#gridlist").jqGrid({
                url: "${ctx}/purReturnOrder/getReturnDrugApplyDataList.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit: false,
              //  height: 255,
                colNames: [
                           'delCompCode',
                           'recpInfoId',
                           'userId',
                           'prodCompcode',
                           'drugCode',
                           '<spring:message code="message.listForm.TuiHuoShuLiang"/>',
                           '退货原因',
                           '<spring:message code="message.listForm.PeiSongQiYe"/>',
                           '<spring:message code="message.listForm.tongYongMing"/>', 
                           '<spring:message code="message.listForm.jiXing"/>',
                           '<spring:message code="message.listForm.guiGe"/>',
                           '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                           '<spring:message code="message.listForm.baoZhuangDanWei"/>', 
                           '<spring:message code="message.listForm.shengChanQiYe"/>',
                           '国药准字',
                           '<spring:message code="message.listForm.PiCiHao"/>',
                           '<spring:message code="message.listForm.DaoHuoShuLiang"/>',
                           '已退货数量',
                           '有效期',
                           '<spring:message code="message.listForm.CaiGouJia"/>'
                           
                          
                           ],
                colModel: [
					{ name: 'delCompCode', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'recpInfoId', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'userId', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'prodCompcode', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'drugCode', index: 'ship',hidden:true, width: 50,align:"right",sortable:false},
					{ name: 'retCount', index: 'sdate', width: 80, align: "center",sortable:false},
					{ name: 'retReason', index: 'sdate', width: 200, align: "center",sortable:false,
						formatter:function(cellvalue, options,row) {
							
     						 return "<input type='text' class='gridText retReason' id='retReason"+row.recpInfoId+"' maxlength='340' value='' />";
     		     		}
					},
					
					{ name: 'delCompName', index: 'ship', width: 190, align: "left", sortable:false},
                    { name: 'qualityspecName', index: 'name', width: 140, align: "left" ,sortable:false },
                    { name: 'FormName', index: 'stock', width: 80 , align: "center",sortable:false},
                    { name: 'specName', index: 'ship', width: 80, align: "left" ,sortable:false},
                    { name: 'factor', index: 'ship', width: 60, align: "center",sortable:false },
                    { name: 'unitName', index: 'ship', width: 40, align: "center",sortable:false },
                    { name: 'prodCompname', index: 'ship', width: 180, align: "left",sortable:false},
                	{ name: 'drugMedicine', index: 'drugMedicine',width: 100, align: "center", sortable:false},
                    { name: 'approveCode', index: 'sdate', width: 100, align: "center",sortable:false},
                    { name: 'totalAmount', index: 'sdate', width: 80, align: "center",sortable:false},
                    { name: 'returnCount', index: 'sdate', width: 80, align: "center",sortable:false},
                   
                    
                    { name: 'credential', index: 'sdate', width: 100, align: "center",sortable:false},
                    { name: 'proPrice', index: 'sdate', width: 80, align: "right",sortable:false}
                    
                ],
                rowNum: 20,
                rowList: [10, 20, 50, 100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                gridComplete: function(){
                   	 //序号列宽度自适应（参数为jqgridID）
                          autoRNWidth("gridlist");
                      	//数据只有一条时默认选中（参数为jqgridID）
                          selectByOneData("gridlist");
                          var ids = jQuery("#gridlist").jqGrid('getDataIDs');
	                  	  for(var i=0;i < ids.length;i++){
	                			var id = ids[i];
	                			var row = $("#gridlist").jqGrid('getRowData', id);
	                			a = "<input type='text'  class='gridText retCount' inputTag='"+id+"' id='retCount"+row.recpInfoId+"' maxlength='11' value='"+(row.totalAmount - row.returnCount) +"' />";
	                    		jQuery("#gridlist").jqGrid('setRowData',ids[i],{retCount:a});
	                			
	                		}
                          $('.retCount').keyup(function(event){
                        	  var num =$(this).attr("inputTag");
                        	  changeSelectChecked("#gridlist",num,$(this).val(),event);
                          }); 
                      	
            	},
                caption: "药品列表",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $("#gridlist").jqGrid("setGridHeight", create());
            $(window).trigger("resize");
        });
       
        
        
        
        //查询
        function gridSerch() {
        	var drugCode = $.trim($("#drugCode").val());
        	var qualityspecName = $.trim($("#qualityspecName").val());
        	var compName = $.trim($("#compName").val());
        	var delCompName = $.trim($("#delCompName").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        		"drugCode":drugCode,
    	        		"drugName":qualityspecName,
    	        		"compName":compName,
    	        		"delCompName":delCompName},
    	        		page:1
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
      //加入退货计划
        function joinReturnPlan(){
        	var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
        	
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("请选择要加入目录的药品！", "<spring:message code="message.HN.alert.type"/>", "warn");
    			return;
    		}
    		
    		var reg = new RegExp(regexEnum.intege1);
            var list = [];
            var drugName ='';
            for (var i=0; i<rowIds.length; i++) {
                rowId = rowIds[i];
                var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
                if ($.trim($("#retCount"+rowdata.recpInfoId).val()) == null || $.trim($("#retCount"+rowdata.recpInfoId).val()) == "" ){
                $.HN.message.alert("<spring:message code="message.yanZheng.QingTianXieTuiHuoShuLiang"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                } 
                if(!reg.test($.trim($("#retCount"+rowdata.recpInfoId).val()))){
                    $.HN.message.alert("<spring:message code="message.yanZheng.TuiHuoShuLiangWeiZhengZhengShu"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
                }
                if( parseInt( $.trim($("#retCount"+rowdata.recpInfoId).val())) > (rowdata.totalAmount - rowdata.returnCount) ){
                    $.HN.message.alert('<spring:message code="message.yanZheng.TuiHuoSLNotChaoGuoDHSL"/><spring:message code="message.yanZheng.ganTanHao"/><spring:message code="message.yanZheng.qingChongXinShuRu"/>', '<spring:message code="message.HN.alert.type"/>', 'warn');
                     return;
                }
                if(i<3){
                	drugName+= rowdata.qualityspecName+"<br/>"
                	if(i==2)
                	drugName+='...';
                	
                }
                 list.push({"retPlanId":rowdata.retPlanId,"retCount":$.trim($("#retCount"+rowdata.recpInfoId).val()),"recpInfoId":rowdata.recpInfoId}); 
            }
    		$.HN.message.confirm('确定加入以下药品吗？<br/>'+drugName, '', '').on(function (e) {
    			if (e) {
    				var list = [];
    	    		for (var i=0; i<rowIds.length; i++) {
    	    			rowId = rowIds[i];
    	    			var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
    	    			list.push({"retReason":$.trim($("#retReason"+rowdata.recpInfoId).val()),"retCount":$.trim($("#retCount"+rowdata.recpInfoId).val()),"recpInfoId":rowdata.recpInfoId}); 
    	    		}
    	    		$.post("${ctx}/purReturnOrder/joinReturnPlan.html", {
    	    			"array" : json.jsonToString(list)
    	    		}, function(result) {
    	    			//var result = eval("("+result+")");
    	    			if (result.success) {
    	    				$.HN.message.alert("成功加入"+list.length+"条药品！", "<spring:message code="message.HN.alert.type"/>", "success");
    	    				$("#gridlist").jqGrid('setGridParam',{  
    	            	        datatype:'json'
    	            	    }).trigger("reloadGrid");
    	    			} else {
    	    				$.HN.message.alert(result.msg, "<spring:message code="message.HN.alert.type"/>", "error");
    	    			}
    	    		}, "json")
    			}
    		});
        }
       
      //回调
		 function operResult(val){
	        	var strs = new Array();
	            strs = val.split("|$");
	            
	            	if (val.length>0) {
						$('#'+strs[0]).val(strs[2]);
					}
					if (val.length>0) {
						$('#'+strs[1]).html(strs[3]);
					}
	        }
        
        
        
    </script>
</body>
</html>